package com.usts2022.classcostrecord;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.usts2022.classcostrecord.Utils.DateFormat;
import com.usts2022.classcostrecord.Utils.HttpUtils;
import com.usts2022.classcostrecord.entity.Question;
import com.usts2022.classcostrecord.entity.Report;
import com.usts2022.classcostrecord.entity.User;

import java.io.IOException;
import java.io.Serializable;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MsgFragment extends Fragment {

    private static final String MSG_REPORT = "MSG_REPORT";
    private static final String MSG_USER = "MSG_USER";

    private Report report;
    private User user;
    private Button upQuestion;
    private TextView startDate, endDate, accPerson;

    //传递参数
    public static MsgFragment newInstance(Report report, User user) {
        MsgFragment fragment = new MsgFragment();
        Bundle args = new Bundle();
        args.putSerializable(MSG_REPORT, report);
        args.putSerializable(MSG_USER, user);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            report = (Report) getArguments().getSerializable(MSG_REPORT);
            user = (User) getArguments().getSerializable(MSG_USER);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg, container, false);
        //绑定
        startDate = view.findViewById(R.id.start_date);
        endDate = view.findViewById(R.id.end_date);
        accPerson = view.findViewById(R.id.acc_person);
        upQuestion = view.findViewById(R.id.up_question);

        if (report.getIsOver() == 1) {
            upQuestion.setEnabled(false);
            Resources resources = getResources();
            Drawable drawable = resources.getDrawable(R.drawable.grey_bg);
            upQuestion.setBackground(drawable);
            upQuestion.setText("质疑时间已截止");
        }


        //点击质疑
        upQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                View alertView = LayoutInflater.from(getActivity()).inflate(R.layout.alert_question, null);
                Button submitQuestion = alertView.findViewById(R.id.submit_question);
                EditText questionContent = alertView.findViewById(R.id.editTextTextMultiLine);
                AlertDialog dialog = builder.setTitle("质疑内容").setView(alertView).show();

                //点击发表
                submitQuestion.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Question question = new Question(report.getId(),user.getId(),questionContent.getText().toString());
                        String url = "/user/putQuestion";
                        try {
                            HttpUtils.post(url, JSON.toJSONString(question), new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {

                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    Looper.prepare();
                                    String str = response.body().string();
                                    if (response.code()!=200 || str==""){
                                        Toast.makeText(getActivity(),"发布失败",Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(getActivity(),"发布成功",Toast.LENGTH_SHORT).show();
                                        dialog.dismiss();
                                    }

                                    Looper.loop();
                                }
                            });
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

        //改变ui
        updateUI();
        return view;
    }

    //改变ui
    private void updateUI() {
        startDate.setText("开始日期: " + DateFormat.dateToStr(report.getDate()));
        endDate.setText("结束日期: " + DateFormat.dateAfterWeek(report.getDate()));
        accPerson.setText("验收人: " + report.getAcceptancePerson());
    }
}